package com.k8sops.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.k8sops.backend.entity.DOSProjectVersion;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 项目版本 Mapper 接口
 */
@Mapper
public interface DOSProjectVersionMapper extends BaseMapper<DOSProjectVersion> {

    /**
     * 更新项目的最新版本号
     *
     * @param projectId 项目ID
     * @param versionCode 版本号
     * @return 影响行数
     */
    @Update("UPDATE dos_project SET latest_version = #{versionNumber} WHERE id = #{projectId}")
    int updateProjectLatestVersion(@Param("projectId") Integer projectId, @Param("versionNumber") String versionNumber);

    /**
     * 根据项目ID获取最新版本
     * 
     * @param projectId 项目ID
     * @return 最新版本信息
     */
    @Select("SELECT version_number, update_log, env_types, project_id, updated_at, created_at, deleted_at " +
           "FROM dos_project_version WHERE project_id = #{projectId} " +
           "ORDER BY created_at DESC LIMIT 1")
    DOSProjectVersion getLatestVersionByProjectId(@Param("projectId") Integer projectId);

    /**
     * 分页查询项目版本
     * 
     * @param page 分页参数
     * @param projectId 项目ID
     * @return 分页结果
     */
    @Select("SELECT version_number, update_log, env_types, project_id, updated_at, created_at, deleted_at " +
           "FROM dos_project_version WHERE project_id = #{projectId} " +
           "ORDER BY created_at DESC")
    IPage<DOSProjectVersion> pageVersionsByProjectId(Page<DOSProjectVersion> page, @Param("projectId") Integer projectId);
}
